/**
 * basicChart.js
 * 获取基础概况的相关数据
 *
 * created by qiuwei
 * version 1.0.0
 * 2016年04月01日
 */

import * as CONSTANTS from '../../constants/API';
import moment from 'moment';
import rp from '../requestProxy';
import DataAccessor, { ENUM_TIME_UNIT, ENUM_DATA_TYPE } from '../dao/DataAccessor';
import { HTTP_RESULT } from '../../constants/protocol';
import { REPLY_DATA } from '../../constants/replyData';

const STUDENT = 'student', TEACHER = 'teacher', SCHOOL = 'school';

// API服务端地址
const url = `${CONSTANTS.HOST}${CONSTANTS.DATA_PACKAGE_ON_SERVER}`;

/**
 * @description 城市视图基础数据
 * @public
 * @param  {string} options.cityId     城市ID
 * @param  {string} options.provinceId 省份ID
 * @param  {array}  options.subjects   科目数组
 * @param  {array}  options.grades     年级数组
 * @param  {string} options.startDate  开始日期范围字符串形式yyyy-mm-dd
 * @param  {string} options.endDate    结束日期范围字符串形式yyyy-mm-dd
 * @return {promise}                   基础数据
 */
export function cityCount({cityId, cityName, provinceId, provinceName, subjects, grades, startDate, endDate}) {
	let dataObjects = cityData(cityId, provinceId, startDate, endDate, subjects, grades);

	// 学生, 教师, 学校
	dataObjects = dataObjects.map((item) => item.timeUnit(ENUM_TIME_UNIT.NONE).packageType('').jsonObject());

	return rp.post(url, dataObjects).then((result) => {
		if (result.code === HTTP_RESULT.SUCCESS) {
			// 城市学生,省份学生,城市教师,省份教师,城市学校,省份学校
			let newArray = [
				[cityName, result.data[0][0].result, result.data[2][0].result, result.data[4][0].result],
				[provinceName, result.data[1][0].result, result.data[3][0].result, result.data[5][0].result]
			]

			return Object.assign({}, REPLY_DATA, result, {
				code: HTTP_RESULT.SUCCESS,
				data: newArray
			});
		}

		return result;
	});
}

/**
 * @description 获取城市视图图表数据
 * @public
 * @param  {string} options.cityId     城市ID
 * @param  {string} options.provinceId 省份IID
 * @param  {array}  options.subjects   科目数组
 * @param  {array}  options.grades     年级数组
 * @param  {string} options.startDate  开始日期范围字符串形式yyyy-mm-dd
 * @param  {string} options.endDate    结束日期范围字符串形式yyyy-mm-dd
 * @return {promise}                   城市数据和所在省份占比数据
 */
export function cityChart({cityId, provinceId, startDate, endDate, subjects, grades}) {
	let dataObjects = cityData(cityId, provinceId, startDate, endDate, subjects, grades);

	dataObjects = dataObjects.map((item) => item.timeUnit(ENUM_TIME_UNIT.DAY).packageType('').jsonObject());

	return rp.post(url, dataObjects)
		.then((result) => {
			if (result.code !== HTTP_RESULT.SUCCESS) 
				throw new Error(result.data.message);

			let [ cityStudentData, provinceStudentData,
					cityTeacherData, provinceTeacherData,
					citySchoolData, provinceSchoolData ] = result.data;

			// if (cityStudentData.length != provinceStudentData.length)
			// 	throw new Error('学生数据长度不一致');
			// if (cityTeacherData.length != provinceTeacherData.length)
			// 	throw new Error('教师数据长度不一致');
			// if (citySchoolData.length != provinceSchoolData.length) 
			// 	throw new Error('学校数据长度不一致');
			// 补全日期
				cityStudentData = completionDate(cityStudentData,startDate,endDate);
				cityTeacherData = completionDate(cityTeacherData,startDate,endDate);
				citySchoolData = completionDate(citySchoolData,startDate,endDate);
			// 计算占比
			let scaleStudentData = scale(cityStudentData, provinceStudentData,startDate,endDate);
			let scaleTeacherData = scale(cityTeacherData, provinceTeacherData,startDate,endDate);
			let scaleSchoolData = scale(citySchoolData, provinceSchoolData,startDate,endDate);

			return Object.assign({}, REPLY_DATA, result, {
				code: HTTP_RESULT.SUCCESS,
				data: [
						[cityStudentData, scaleStudentData],
						[cityTeacherData, scaleTeacherData],
						[citySchoolData, scaleSchoolData]
					]
			});
		});
}

/**
 * @description 区县视图的基础数据
 * @public
 * @param  {string} options.countyId  区县ID
 * @param  {string} options.cityId    城市ID
 * @param  {array}  options.subjects  科目数组
 * @param  {array}  options.grades    年级数组
 * @param  {string} options.startDate 开始日期范围字符串形式yyyy-mm-dd
 * @param  {string} options.endDate   结束日期范围字符串形式yyyy-mm-dd
 * @return {promise}                  基础数据
 */
export function countyCount({countyId, countyName, cityId, cityName, startDate, endDate, subjects, grades}) {
	let dataObjects = countyData(countyId, cityId, startDate, endDate, subjects, grades);

	dataObjects = dataObjects.map((item) => item.timeUnit(ENUM_TIME_UNIT.NONE).packageType('').jsonObject());

	return rp.post(url, dataObjects).then((result) => {
		if (result.code === HTTP_RESULT.SUCCESS) {
			// 区县学生,城市学生,区县教师,城市教师,区县学校,城市学校
			let newArray = [
				[countyName, result.data[0][0].result, result.data[2][0].result, result.data[4][0].result],
				[cityName, result.data[1][0].result, result.data[3][0].result, result.data[5][0].result]
			]

			return Object.assign({}, REPLY_DATA, result, {
				code: HTTP_RESULT.SUCCESS,
				data: newArray
			});
		}

		return result;
	});
}

/**
 * @description 获取区县视图图表数据
 * @public
 * @param  {string} options.countyId   区县ID
 * @param  {string} options.cityId 	   城市ID
 * @param  {array}  options.subjects   科目数组
 * @param  {array}  options.grades     年级数组
 * @param  {string} options.startDate  开始日期范围字符串形式yyyy-mm-dd
 * @param  {string} options.endDate    结束日期范围字符串形式yyyy-mm-dd
 * @return {promise}                   区县数据和所在城市占比数据
 */
export function countyChart({countyId, cityId, startDate, endDate, subjects, grades}) {
	let dataObjects = countyData(countyId, cityId, startDate, endDate, subjects, grades);

	dataObjects = dataObjects.map((item) => item.timeUnit(ENUM_TIME_UNIT.DAY).packageType('').jsonObject());

	return rp.post(url, dataObjects)
		.then((result) => {
			if (result.code !== HTTP_RESULT.SUCCESS) 
				throw new Error(result.data.message);

			let [ countyStudentData, cityStudentData,
					countyTeacherData, cityTeacherData,
					countySchoolData, citySchoolData ] = result.data;

			// if (countyStudentData.length != cityStudentData.length)
			// 	throw new Error('学生数据长度不一致');
			// if (countyTeacherData.length != cityTeacherData.length)
			// 	throw new Error('教师数据长度不一致');
			// if (countySchoolData.length != citySchoolData.length)
				// throw new Error('学校数据长度不一致');
			// 补全日期
			countyStudentData = completionDate(countyStudentData,startDate,endDate);
			countyTeacherData = completionDate(countyTeacherData,startDate,endDate);
			countySchoolData = completionDate(countySchoolData,startDate,endDate);
			// 计算占比
			let scaleStudentData = scale(countyStudentData, cityStudentData,startDate,endDate);
			let scaleTeacherData = scale(countyTeacherData, cityTeacherData,startDate,endDate);
			let scaleSchoolData = scale(countySchoolData, citySchoolData,startDate,endDate);

			return Object.assign({}, REPLY_DATA, result, {
				code: HTTP_RESULT.SUCCESS,
				data: [
					[countyStudentData, scaleStudentData],
					[countyTeacherData, scaleTeacherData],
					[countySchoolData, scaleSchoolData]
				]
			});
		});
}

/**
 * @description 学校视图的基础数据
 * @public
 * @param  {string} options.schoolId  学校ID
 * @param  {string} options.countyId  区县ID
 * @param  {array}  options.subjects  科目数组
 * @param  {array}  options.grades    年级数组
 * @param  {string} options.startDate 开始日期范围字符串形式yyyy-mm-dd
 * @param  {string} options.endDate   结束日期范围字符串形式yyyy-mm-dd
 * @return {promise}                  基础数据
 */
export function schoolCount({schoolId, schoolName, countyId, countyName, startDate, endDate, subjects, grades}) {
	let dataObjects = schoolData(schoolId, countyId, startDate, endDate, subjects, grades);

	dataObjects = dataObjects.map((item) => item.timeUnit(ENUM_TIME_UNIT.NONE).packageType('').jsonObject());

	return rp.post(url, dataObjects).then((result) => {
		if (result.code === HTTP_RESULT.SUCCESS) {
			
			let newArray = [
				[schoolName, result.data[0][0].result, result.data[2][0].result, result.data[4][0].result],
				[countyName, result.data[1][0].result, result.data[3][0].result, result.data[5][0].result]
			]

			return Object.assign({}, REPLY_DATA, result, {
				code: HTTP_RESULT.SUCCESS,
				data: newArray
			});
		}

		return result;
	});
}

/**
 * @description 获取区学校图图表数据
 * @public
 * @param  {string} options.countyId   学校ID
 * @param  {string} options.cityId 	   区县ID
 * @param  {array}  options.subjects   科目数组
 * @param  {array}  options.grades     年级数组
 * @param  {string} options.startDate  开始日期范围字符串形式yyyy-mm-dd
 * @param  {string} options.endDate    结束日期范围字符串形式yyyy-mm-dd
 * @return {promise}                   学校数据和所在区县占比数据
 */
export function schoolChart({schoolId, countyId, startDate, endDate, subjects, grades}) {
	let dataObjects = schoolData(schoolId, countyId, startDate, endDate, subjects, grades);

	dataObjects = dataObjects.map((item) => item.timeUnit(ENUM_TIME_UNIT.DAY).packageType('').jsonObject());

	return rp.post(url, dataObjects)
		.then((result) => {
			if (result.code !== HTTP_RESULT.SUCCESS) 
				throw new Error(result.data.message);

			let [ schooStudentlData, countyStudentData,
					schoolTeacherData, countyTeacherData,
					schoolSchoolData, countySchoolData ] = result.data;

			// if (schooStudentlData.length != countyStudentData.length)
			// 	throw new Error('学生数据长度不一致');
			// if (schoolTeacherData.length != countyTeacherData.length)
			// 	throw new Error('教师数据长度不一致');
			// if (schoolSchoolData.length != countySchoolData.length)
			// 	throw new Error('学校数据长度不一致');
			// 补全日期
			schooStudentlData = completionDate(schooStudentlData,startDate,endDate);
			schoolTeacherData = completionDate(schoolTeacherData,startDate,endDate);
			schoolSchoolData = completionDate(schoolSchoolData,startDate,endDate);
			// 计算占比
			let scaleStudentData = scale(schooStudentlData, countyStudentData,startDate,endDate);
			let scaleTeacherData = scale(schoolTeacherData, countyTeacherData,startDate,endDate);
			let scaleSchoolData = scale(schoolSchoolData, countySchoolData,startDate,endDate);

			return Object.assign({}, REPLY_DATA, result, {
				code: HTTP_RESULT.SUCCESS,
				data: [
					[schooStudentlData, scaleStudentData],
					[schoolTeacherData, scaleTeacherData],
					[schoolSchoolData, scaleSchoolData]
				]
			});
		});
}

/**
 * @description 城市视图数据详情
 * @public
 * @param  {string} options.cityId    城市ID
 * @param  {string} options.startDate 开始日期
 * @param  {string} options.endDate   结束日期
 * @param  {array}  options.subjects  科目数组
 * @param  {array}  options.grades    年级数组
 * @param  {string} options.orderBy   排序字段(student, teacher, school)
 * @param  {bool} 	options.desc 	  是否降序排列(true), 默认升序排列(false)
 * @return {promise}                  详情数据
 */
export function cityDetail({cityId, startDate, endDate, orderBy, desc, subjects, grades}) {
	let dataObjects = detail(startDate, endDate, subjects, grades);

	dataObjects[0].multiUnit(`${STUDENT}-county_id`);
	dataObjects[1].multiUnit(`${TEACHER}-county_id`);
	dataObjects[2].multiUnit(`${SCHOOL}-county_id`);

	dataObjects = dataObjects.map((item) => item.timeUnit(ENUM_TIME_UNIT.NONE).packageType('').city(cityId).jsonObject());

	return rp.post(url, dataObjects).then((result) => {
		if (result.code === HTTP_RESULT.SUCCESS) {
			// result.data is [[], [], []];

			if (orderBy === 'student')
				orderBy = 1;
			else if (orderBy === 'teacher') 
				orderBy = 2;
			else if (orderBy === 'school') 
				orderBy = 3;
			// else 
			// 	throw new Error(`未定义的排序字段: ${orderBy}`);

			var newArray = combine(result.data, 'show_name', 'result');
			// 排序，区分是否有orderBy
			if(!orderBy) {
				newArray.sort((one, another) => {
					return (another[1] + another[2]) - (one[1] + one[2]);
				});
			} else {
				newArray.sort((one, another) => {
					return one[orderBy] - another[orderBy];
				});
			}

			if (desc)
				newArray.reverse();

			return Object.assign({}, REPLY_DATA, result, {
				code: HTTP_RESULT.SUCCESS,
				data: newArray
			});
		}

		// 如果返回结果非预期, 将数据原样发送给客户端
		return result;	
	});
}

/**
 * @description 区县视图数据详情
 * @public
 * @param  {string} options.countyId  区县ID
 * @param  {array}  options.subjects  科目数组
 * @param  {array}  options.grades    年级数组
 * @param  {string} options.startDate 开始日期范围字符串形式yyyy-mm-dd
 * @param  {string} options.endDate   结束日期范围字符串形式yyyy-mm-dd
 * @param  {string} options.orderBy   排序字段(student, teacher, school)
 * @param  {bool} 	options.desc 	  是否降序排列(true), 默认升序排列(false)
 * @return {promise}                  详情数据
 */
export function countyDetail({countyId, startDate, endDate, orderBy, desc, subjects, grades}) {
	let dataObjects = detail(startDate, endDate, subjects, grades);
	dataObjects.length = 2;

	dataObjects[0].multiUnit(`${STUDENT}-school_id`);
	dataObjects[1].multiUnit(`${TEACHER}-school_id`);
	// dataObjects[2].multiUnit(`${SCHOOL}-school_id`);

	dataObjects = dataObjects.map((item) => item.timeUnit(ENUM_TIME_UNIT.NONE).packageType('').county(countyId).jsonObject());

	return rp.post(url, dataObjects).then((result) => {
		// [[], []]
		if (result.code === HTTP_RESULT.SUCCESS) {
			if (orderBy === 'student')
				orderBy = 1;
			else if (orderBy === 'teacher') 
				orderBy = 2;
			// else if (orderBy === 'school') 
			// 	orderBy = 3;
			// else 
			// 	throw new Error(`未定义的排序字段: ${orderBy}`);

			var newArray = combine(result.data, 'show_name', 'result');

			// 区县视图详情下只提供三项数据，手动控制length
			newArray = newArray.map((item) => {
				item.length = 3;
				return item;
			});

			// 排序，区分是否有orderBy
			if(!orderBy) {
				newArray.sort((one, another) => {
					return (another[1] + another[2]) - (one[1] + one[2]);
				});
			} else {
				newArray.sort((one, another) => {
					return one[orderBy] - another[orderBy];
				});
			}

			if (desc)
				newArray.reverse();

			return Object.assign({}, REPLY_DATA, result, {
				code: HTTP_RESULT.SUCCESS,
				data: newArray
			});
		}

		return result;
	});
}

/**
 * @description 根据表名获取查询参数对象
 * @private
 * @param  {string} title     表名
 * @param  {string} startDate 开始日期
 * @param  {string} endDate   结束日期
 * @param  {array}  subjects  科目数组
 * @param  {array}  grades    年级数组
 * @return {DataAccessor}     数据访问参数对象
 */
function getDataObjectByTitle(title, startDate, endDate, subjects, grades) {
	let dao = new DataAccessor(title, `${title}_id`)
		.dataType(ENUM_DATA_TYPE.COUNT)
		.timeUnit(ENUM_TIME_UNIT.WEEK)
		.dateRange(startDate, endDate);

	if (subjects && subjects.length)
		dao.subject(subjects);
	if (grades && grades.length) 
		dao.grade(grades);

	return dao;
}

/**
 * @description 获取数据详情的查询参数
 * @private
 * @param  {string} startDate 开始日期
 * @param  {string} endDate   结束日期
 * @param  {array}  subjects  科目数组
 * @param  {array}  grades    年级数组
 * @return {array}            查询参数
 */
function detail(startDate, endDate, subjects, grades) {
	let student = getDataObjectByTitle(STUDENT, startDate, endDate, null, grades);
	let teacher = getDataObjectByTitle(TEACHER, startDate, endDate, subjects, grades);
	let school = getDataObjectByTitle(SCHOOL, startDate, endDate);

	return [student, teacher, school];
}

/**
 * 获得城市/省份数据的查询参数对象
 * @private
 * @param  {string} cityId     城市ID
 * @param  {string} provinceId 省份ID
 * @param  {array}  subjects  科目数组
 * @param  {array}  grades    年级数组
 * @param  {string} startDate  开始日期
 * @param  {string} endDate    结束日期
 * @return {array}             查询参数集合
 */
function cityData(cityId, provinceId, startDate, endDate, subjects, grades) {
	let cityOfStudent = getDataObjectByTitle(STUDENT, startDate, endDate, null, grades).city(cityId);
	let provinceOfStudent = getDataObjectByTitle(STUDENT, startDate, endDate, null, grades).province(provinceId);

	let cityOfTeacher = getDataObjectByTitle(TEACHER, startDate, endDate, subjects, grades).city(cityId);
	let provinceOfTeacher = getDataObjectByTitle(TEACHER, startDate, endDate, subjects, grades).province(provinceId);

	let cityOfSchool = getDataObjectByTitle(SCHOOL, startDate, endDate).city(cityId);
	let provinceOfSchool = getDataObjectByTitle(SCHOOL, startDate, endDate).province(provinceId);

	return [cityOfStudent, provinceOfStudent, 
				cityOfTeacher, provinceOfTeacher,
				cityOfSchool, provinceOfSchool];
}

/**
 * @description 获得区县/城市数据的查询参数对象
 * @private
 * @param  {string} countyId  区县ID
 * @param  {string} cityId    城市ID
 * @param  {array}  subjects  科目数组
 * @param  {array}  grades    年级数组
 * @param  {string} startDate 开始日期
 * @param  {string} endDate   结束日期
 * @return {array}            查询参数集合
 */
function countyData(countyId, cityId, startDate, endDate, subjects, grades) {
	let countyOfStudent = getDataObjectByTitle(STUDENT, startDate, endDate, null, grades).county(countyId);
	let cityOfStudent = getDataObjectByTitle(STUDENT, startDate, endDate, null, grades).city(cityId);

	let countyOfTeacher = getDataObjectByTitle(TEACHER, startDate, endDate, subjects, grades).county(countyId);
	let cityOfTeacher = getDataObjectByTitle(TEACHER, startDate, endDate, subjects, grades).city(cityId);

	let countyOfSchool = getDataObjectByTitle(SCHOOL, startDate, endDate).county(countyId);
	let cityOfSchool = getDataObjectByTitle(SCHOOL, startDate, endDate).city(cityId);

	return [countyOfStudent, cityOfStudent,
				countyOfTeacher, cityOfTeacher,
				countyOfSchool, cityOfSchool];
}

/**
 * @description 获得学校/区县数据的查询参数对象
 * @private
 * @param  {string} schoolId  学校ID
 * @param  {string} countyId  区县ID
 * @param  {array}  subjects  科目数组
 * @param  {array}  grades    年级数组
 * @param  {string} startDate 开始日期
 * @param  {string} endDate   结束日期
 * @return {array}            查询参数集合
 */
function schoolData(schoolId, countyId, startDate, endDate, subjects, grades) {
	let schoolOfStudent = getDataObjectByTitle(STUDENT, startDate, endDate, null, grades).school(schoolId);
	let countyOfStudent = getDataObjectByTitle(STUDENT, startDate, endDate, null, grades).county(countyId);

	let schoolOfTeacher = getDataObjectByTitle(TEACHER, startDate, endDate, subjects, grades).school(schoolId);
	let countyOfTeacher = getDataObjectByTitle(TEACHER, startDate, endDate, subjects, grades).county(countyId);

	let schoolOfSchool = getDataObjectByTitle(SCHOOL, startDate, endDate).school(schoolId);
	let countyOfSchool = getDataObjectByTitle(SCHOOL, startDate, endDate).county(countyId);

	return [schoolOfStudent, countyOfStudent,
				schoolOfTeacher, countyOfTeacher,
				schoolOfSchool, countyOfSchool];
}

/**
 * 补全服务器没有的日期
 * @param  {[type]} numeratorDataSet [原新增人数数组]
 * @param  {[type]} startDate        [description]
 * @param  {[type]} endDate          [description]
 * @return {[type]}                  [description]
 */
function completionDate(numeratorDataSet,startDate,endDate) {
	let timeSheetSet = new Set();
	//开始时间，截止时间时间戳
	let start = moment(startDate).valueOf(),
		end = moment(endDate).valueOf();
	//计算间隔天数
	let days = Math.ceil((end - start-100000)/(1000*3600*24));
	for (let i=0;i<=days;i++){
		let obj = new Object();
		let timeSheetValue = moment(endDate).subtract(i,'days').format('YYYY-MM-DD');
		timeSheetSet.add(timeSheetValue)
	};
	let numeratorDataMap = numeratorDataSet.reduce((map, item) => {
		timeSheetSet.add(item.time_sheet);
		map[item.time_sheet] = item.result;
		return map;
	}, {});

	let timeSheets = Array.from(timeSheetSet).reverse(), numeratorResult, denominatorResult;
	return timeSheets.map((key) => {
		numeratorResult = numeratorDataMap[key] || 0;

		let time_sheet = key, result = numeratorResult;
		return { time_sheet, result };
	})
	return [];
} 
/**
 * @description 计算数据占比
 * @private
 * @param  {array} numeratorDataSet   分子位数据集
 * @param  {array} denominatorDataSet 分母位数据集
 * @return {array}                    分子数据和占比数据
 */
function scale(numeratorDataSet, denominatorDataSet, startDate, endDate) {
	let timeSheetSet = new Set();
	let start = moment(startDate).valueOf(),
		end = moment(endDate).valueOf();
	//计算间隔天数
	let days = Math.ceil((end - start-100000)/(1000*3600*24));
	for (let i=0;i<=days;i++){
		let timeSheetValue = moment(endDate).subtract(i,'days').format('YYYY-MM-DD');	
		timeSheetSet.add(timeSheetValue)
	}
	// 补全可能缺失的数据
	let numeratorDataMap = numeratorDataSet.reduce((map, item) => {
		timeSheetSet.add(item.time_sheet);
		map[item.time_sheet] = item.result;
		return map;
	}, {});
	let denominatorDataMap = denominatorDataSet.reduce((map, item) => {
		timeSheetSet.add(item.time_sheet);
		map[item.time_sheet] = item.result;
		return map;
	}, {});

	let timeSheets = Array.from(timeSheetSet).reverse(), numeratorResult, denominatorResult;
	return timeSheets.map((key) => {
		numeratorResult = numeratorDataMap[key] || 0;
		denominatorResult = denominatorDataMap[key] || 0;

		let x = key, y = 0;
		if (denominatorResult)
			y = (numeratorResult / denominatorResult) * 100;

		return { x, y };
	});

	// 求比例
	// let dataItem, numeratorResult, denominatorResult;
	// let scaleDataSet = numeratorDataSet.reduce((collect, item ,index) => {

	// 	numeratorResult = parseFloat(item.result);
	// 	denominatorResult = parseFloat(denominatorDataSet[index].result);
	// 	dataItem = { 'x': item.time_sheet , y: (numeratorResult / denominatorResult) * 100 };

	// 	collect.push(dataItem);
	// 	return collect;
	// }, []);

	return [];
}

/**
 * @description 合并二维数组中的对象, 返回一个新的二维数组, 相当于纵转横
 * @param  {array} arr        原始数组
 * @param  {string} keyName   对象键名
 * @param  {string} valueName 对象值名
 * @return {array}            新的二维数组
 */
function combine(arr, keyName, valueName) {
	let map = {}, item, current; 
	// item is ['', 0, 0, 0];

	// 顺序分别是, 学生数据, 教师数据, 学校数据
	for (let columnIndex = 0; columnIndex < arr.length; columnIndex++) {
		current = arr[columnIndex];

		for (let rowIndex = 0; rowIndex < current.length; rowIndex++) {
			item = current[rowIndex];

			if (!map[item[keyName]])
				map[item[keyName]] = [item[keyName], 0, 0, 0];

			map[item[keyName]][columnIndex+1] = parseInt(item[valueName]);
		}
	}

	return Object.values(map);
}